#include <string.h>
#include "app_pwr.h"
#include "drv_usart.h"
#include "drv_pwm.h"
#include "app_config.h"
#include "nst_log.h"
#include "sys_manager.h"
#include "app_sys.h"
#include "app_misc.h"

#ifndef PWR_ON_FIRST_DELAY_MS
#define PWR_ON_FIRST_DELAY_MS                   300
#endif

#ifndef PWR_ON_SECOND_DELAY_MS
#define PWR_ON_SECOND_DELAY_MS                  500
#endif

#ifndef PWR_ON_CMD
#define PWR_ON_CMD                              0x0A
#endif

PWR_MODE pwr_state = PWR_OFF; 

void app_power_on(void) //开机代码
{
    if(pwr_state == PWR_ON)
    {
        return;
    }

    pwr_state = PWR_ON;
    system_status.power.power_sta = PWR_ON;
    app_config_mode(WORK_PWR_ON);

    // 标记需要执行硬件操作
    system_status.power.pending_switch = true;
}

void app_power_off(void)
{
    if(pwr_state == PWR_OFF)
    {
        return;
    }

    drv_pwm_stop(TIM_CHANNEL_1);
    drv_pwm_stop(TIM_CHANNEL_2);
    HAL_GPIO_WritePin(DZQ_YANY_GPIO_Port, DZQ_YANY_Pin, GPIO_PIN_RESET); // 输出低电平
    HAL_GPIO_WritePin(DZQ_RUOY_GPIO_Port, DZQ_RUOY_Pin, GPIO_PIN_RESET); // 输出低电平

    pwr_state = PWR_OFF;
    system_status.power.power_sta = PWR_OFF;

    app_config_mode(WORK_PWR_OFF);
    // 标记需要执行硬件操作
    system_status.power.pending_switch = true;
}

bool is_power_on(void)
{
    return pwr_state == PWR_ON;
}

bool is_power_off(void)
{
    return pwr_state == PWR_OFF;
}


void app_handle_pwr_switch_info_output(void)
{
    uint8_t pwr_msg;

    switch (pwr_state)
    {
    case PWR_OFF:
        {
            pwr_msg = 0x0B;
            app_buzzer_trigger(APP_BUZZER_POWER_OFF_DURATION_MS);
            delay_ms(300);
            drv_usart_send_data(USART1_PORT, &pwr_msg, 1);
            delay_ms(200);
            app_fm_stop();
            char *msg = "CLOSE";
            drv_usart_send_data(USART3_PORT, (uint8_t*)msg, strlen(msg));
        }
        break;
    case PWR_ON:
        {
            pwr_msg = PWR_ON_CMD;
            // 步骤1：发送开机指令和触发蜂鸣器
            drv_usart_send_data(USART1_PORT, &pwr_msg, 1);
            app_buzzer_trigger(APP_BUZZER_POWER_ON_DURATION_MS);
            
            // 步骤2：第一次延时
            delay_ms(PWR_ON_FIRST_DELAY_MS);
            
            // 步骤3：触发继电器
            app_fm_start();
            
            // 步骤4：第二次延时
            delay_ms(PWR_ON_SECOND_DELAY_MS);
            
            // 步骤5：发送OPEN消息到串口3(数据转发)
            char *msg = "OPEN";
            drv_usart_send_data(USART3_PORT, (uint8_t*)msg, strlen(msg));
        }
        break;
    default:
        break;
    }
}

void app_handle_pwr_down(PWR_SOURCE pwr_src)
{

    switch (pwr_src)
    {
    case PWR_5V:
        {
            if(is_power_on())
            {
                app_power_off();
            }
            system_status.power.low_voltage = 1; 
            system_status.power.wait_report = 1; 
            APP_LOG_PRINT("app_handle_pwr_down: pwr source 5V down!");
        }
        break;
    case PWR_25V:
        {
            if(is_power_on())
            {
                app_power_off();
            }
            system_status.power.low_voltage = 1; 
            system_status.power.wait_report = 1; 
            APP_LOG_PRINT("app_handle_pwr_down: pwr source 25V down!");
        }
        break;
    default:
        break;
    }
}

void app_handle_pwr_normal(void)
{
    system_status.power.low_voltage = 0; 
}


